package com.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.store.common.Result;
import com.store.dto.ProductCreateRequest;
import com.store.dto.ProductQueryRequest;
import com.store.dto.ProductUpdateRequest;
import com.store.service.ProductService;
import com.store.vo.ProductVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 * 
 * @author Store Team
 */
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    /**
     * 分页查询商品列表
     */
    @PostMapping("/list")
    public Result<IPage<ProductVO>> getProductList(@RequestBody ProductQueryRequest request) {
        try {
            IPage<ProductVO> page = productService.getProductList(request);
            return Result.success("查询成功", page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID获取商品详情
     */
    @GetMapping("/{id}")
    public Result<ProductVO> getProductById(@PathVariable Long id) {
        try {
            ProductVO product = productService.getProductById(id);
            return Result.success("查询成功", product);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建商品（管理员权限）
     */
    @PostMapping
    public Result<ProductVO> createProduct(@Valid @RequestBody ProductCreateRequest request) {
        try {
            ProductVO product = productService.createProduct(request);
            return Result.success("创建成功", product);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新商品（管理员权限）
     */
    @PutMapping
    public Result<ProductVO> updateProduct(@Valid @RequestBody ProductUpdateRequest request) {
        try {
            ProductVO product = productService.updateProduct(request);
            return Result.success("更新成功", product);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除商品（管理员权限）
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteProduct(@PathVariable Long id) {
        try {
            productService.deleteProduct(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量删除商品（管理员权限）
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteProducts(@RequestBody List<Long> ids) {
        try {
            productService.batchDeleteProducts(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取热销商品
     */
    @GetMapping("/hot")
    public Result<List<ProductVO>> getHotProducts(@RequestParam(required = false, defaultValue = "10") Integer limit) {
        try {
            List<ProductVO> products = productService.getHotProducts(limit);
            return Result.success("查询成功", products);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取库存预警商品（管理员权限）
     */
    @GetMapping("/stock-alert")
    public Result<List<ProductVO>> getStockAlertProducts() {
        try {
            List<ProductVO> products = productService.getStockAlertProducts();
            return Result.success("查询成功", products);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

}







